<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    }


    public function prize(){
        $this->show();
    }

    public function getRandprize(){
        $prize_arr = array(
            '0' => array('id' => 1, 'prize' => '一等奖', 'v' => 100),
            '1' => array('id' => 2, 'prize' => '二等奖', 'v' => 30),
            '2' => array('id' => 3, 'prize' => '三等奖', 'v' => 5),
            '3' => array('id' => 4, 'prize' => '四等奖', 'v' => 50),
            '4' => array('id' => 5, 'prize' => '五等奖', 'v' => 5),
            '5' => array('id' => 6, 'prize' => '六等奖', 'v' => 5),
            '6' => array('id' => 7, 'prize' => '七等奖', 'v' => 5),
            '7' => array('id' => 8, 'prize' => '八等奖', 'v' => 5),
            '8' => array('id' => 9, 'prize' => '九等奖', 'v' => 5),
            '9' => array('id' => 10, 'prize' => '十等奖', 'v' => 66),
            '10' => array('id' => 11, 'prize' => '十一等奖', 'v' => 25),
            '11' => array('id' => 12, 'prize' => '十二等奖', 'v' => 25),
        );
        foreach ($prize_arr as $k=>$v) {
            $arr[$v['id']] = $v['v'];
        }
        $prize_id = $this->get_rand($arr);//根据概率获取奖项id 
        foreach($prize_arr as $k=>$v){ //获取前端奖项位置
            if($v['id'] == $prize_id){
             $prize_site = $k;
             break;
            }
        }
        $res = $prize_arr[$prize_id - 1]; //中奖项 
        /*if($prize_id == 7){
            $arr_number = array(3,9);
            $prize_site = $arr_number[array_rand($arr_number,1)];
        }*/
        $data['prize_name'] = $res['prize'];
        $data['prize_site'] = $prize_site;//前端奖项从-1开始
        $data['prize_id'] = $res['id'];
        $this->ajaxReturn($data);
    }

    public function  get_rand($proArr) {
        $result = '';
        //概率数组的总概率精度
        $proSum = array_sum($proArr);
        //概率数组循环
        foreach ($proArr as $key => $proCur) {
            /*随机从1到概率值*/
            $randNum = mt_rand(1, $proSum);
            /* 如果概率小于或等于当前$proCur概率值
             *则赋值给$result结束数组循环
             *否则总概率减去当前概率剩下概率继续循环
            */
            if ($randNum <= $proCur) {
                $result = $key;
                break;
            } else {
                $proSum-= $proCur;
            }
        }
        unset($proArr);
        // 返回中奖id
        return $result;
    }

}